package com.doumuxie.util;

import com.doumuxie.dto.ProcessDto;

import java.util.List;

/**
 * @author doumuxie https://gitee.com/doumuxie/
 * @version 1.0
 * @date 2020/9/26 9:05
 * @description 指定 List 转 二维数组
 **/
public class DataUtil {
    public synchronized static <T> String[][] toArray(List<T> data) {
        int size = data.size();
        if (size <= 0) {
            return new String[][]{};
        }
        //动态分配一个二维数组的
        String[][] o = new String[size][data.get(0).toString().split(",").length];
        for (int i = 0; i < size; i++) {
            ProcessDto process = (ProcessDto) data.get(i);
            o[i][0] = process.getId();
            o[i][1] = process.getName();
            o[i][2] = process.getLevel() + "";
            o[i][3] = process.getTimeSliceCount() + "";
            String state = process.getState();
            String stateStr;
            switch (state) {
                case SysCode.PROCESS_STATE.NEW:
                    stateStr = "就绪";
                    break;
                case SysCode.PROCESS_STATE.BLOCK:
                    stateStr = "阻塞";
                    break;
                case SysCode.PROCESS_STATE.RUN:
                    stateStr = "执行";
                    break;
                default:
                    stateStr = "消亡";
                    break;
            }
            o[i][4] = stateStr;
            o[i][5] = process.getData() + "";
        }
        return o;
    }

    public static Integer[][] copyArray(Integer[][] source) {
        if (source.length == 0 || source[0].length == 0) {
            return new Integer[][]{};
        }
        Integer[][] result = new Integer[source.length][source[0].length];
        for (int i = 0; i < source.length; i++) {
            for (int j = 0; j < source[i].length; j++) {
                result[i][j] = source[i][j];
            }
        }
        return result;
    }

    public static Integer[] copyArray(Integer[] source) {
        if (source.length == 0) {
            return new Integer[]{};
        }
        Integer[] result = new Integer[source.length];
        for (int i = 0; i < source.length; i++) {
            result[i] = source[i];
        }
        return result;
    }
}
